package userclasses;

import generated.StateMachineBase;
import com.sun.lwuit.*;
import com.sun.lwuit.list.*;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Daniel Lingenfelter
 */

public class StateMachine extends StateMachineBase {
    Form mainForm, passDial, pickDial, dropDial, TransForm, Report;
    Request req;
    Response res;
    SystemConnect SysCon;

    public StateMachine(String resFile) {
        super(resFile);
    }

    /**
     * this method should be used to initialize variables.
     */
    protected void initVars() {
        SysCon = new SystemConnect();
    }

    protected boolean initListModelPassengerList(List cmp) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.initListModelPassengerList(cmp);
        DefaultListModel PassList = new DefaultListModel(SysCon.PassListHash);
        cmp.setModel(PassList);

        return true;
    }

    protected boolean initListModelPickUpList(List cmp) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.initListModelPassengerList(cmp);
        DefaultListModel PassengersListModel = new DefaultListModel(SysCon.PickUpHash);
        cmp.setModel(PassengersListModel);

        return true;
    }

    protected boolean initListModelDropOffList(List cmp) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.initListModelPassengerList(cmp);
        DefaultListModel PassengersListModel = new DefaultListModel(SysCon.DropOffHash);
        cmp.setModel(PassengersListModel);

        return true;
    }

    protected void beforeMain(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeMain(f);
        mainForm = f;
        findEditPass(f).setText("Number of Passengers:");
        findEditPickUp(f).setText("Pick Up Location:");
        findEditDropOff(f).setText("Drop Off Location:");
        SysCon.connect();
        updateStatus(f, SysCon);
    }

    protected void beforePassengers(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforePassengers(f);
        passDial = f;
    }

    protected void beforePickUp(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforePickUp(f);
        pickDial = f;
    }

    protected void beforeDropOff(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeDropOff(f);
        dropDial = f;
    }

    protected boolean onPassengersDone() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onPassengersDone();
        Form f = passDial;
        int sel = findPassengerList(f).getSelectedIndex();
        SysCon.setPassOption(SysCon.PassOpArray, sel);
        updateOptions();
        return val;
    }

    protected boolean onPickUpDone() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onPickUpDone();
        Form f = pickDial;
        int sel = findPickUpList(f).getSelectedIndex();
        SysCon.setPickStationOption(SysCon.PickUpArray, sel);
        updateOptions();
        return val;
    }

    protected boolean onDropOffDone() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onDropOffDone();
        Form f = dropDial;
        int sel = findDropOffList(f).getSelectedIndex();
        SysCon.setDropStationOption(SysCon.DropOffArray, sel);
        updateOptions();
        return val;
    }

    protected void postRequesting(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.postRequesting(f);
        req = new Request(SysCon.passValue, SysCon.pickValue, SysCon.dropValue);
        res = req.getResponse();
    }

    protected void beforeRideOverview(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeRideOverview(f);
        findVehicleNum(f).setText(res.VehicleNum);
        int time = res.PickTime;
        int min = (int)(time/60000);
        int sec = (int)((time%60000)/1000);
        String minutes = String.valueOf(min);
        String seconds = String.valueOf(sec);
        if(minutes.length()==1){
            minutes = "0"+minutes;
        }
        if(seconds.length()==1){
            seconds = "0"+seconds;
        }
        findCountdownLabel(f).setText(minutes + ":" + seconds);
        findRideInfo2(f).setEnabled(false);
        findReadyButton(f).setEnabled(false);
    }

    protected void postRideOverview(final Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.postRideOverview(f);
        class MyTimerTask extends TimerTask{
            int min, sec, time;
            public void run() {
                time = res.PickTime;
                while(time >= 0){
                    min = (int)(time/60000);
                    sec = (int)((time%60000)/1000);
                    String minutes = String.valueOf(min);
                    String seconds = String.valueOf(sec);
                    if(minutes.length()==1){
                        minutes = "0"+minutes;
                    }
                    if(seconds.length()==1){
                        seconds = "0"+seconds;
                    }
                    findCountdownLabel(f).setText(minutes + ":" + seconds);
                    time = time - 1000;

                    try{
                    //do what you want to do before sleeping
                        Thread.sleep(1000);//sleep for 1000 ms
                    //do what you want to do after sleeptig
                    }
                    catch(Exception ie){
                    }
                }
                findRideInfo2(f).setEnabled(true);
                findReadyButton(f).setEnabled(true);
            }
        }
        Timer t = new Timer();
        TimerTask task = new MyTimerTask();
        t.schedule(task, 1000);
    }

    protected void beforeTransporting(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeTransporting(f);
        findDestInfo2(f).setEnabled(false);
        findDoneButton(f).setEnabled(false);
        TransForm = f;
        findStationNum(f).setText(SysCon.dropSelected);
        int time = res.TransportTime;
        int min = (int)(time/60000);
        int sec = (int)((time%60000)/1000);
        String minutes = String.valueOf(min);
        String seconds = String.valueOf(sec);
        if(minutes.length()==1){
            minutes = "0"+minutes;
        }
        if(seconds.length()==1){
            seconds = "0"+seconds;
        }
        findCountdownTimer(f).setText(minutes + ":" + seconds);
    }

    protected void postTransporting(final Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.postTransporting(f);
        class MyTimerTask extends TimerTask{
            int min, sec, time;
            public void run() {
                time = res.TransportTime;
                while(time >= 0){
                    min = (int)(time/60000);
                    sec = (int)((time%60000)/1000);
                    String minutes = String.valueOf(min);
                    String seconds = String.valueOf(sec);
                    if(minutes.length()==1){
                        minutes = "0"+minutes;
                    }
                    if(seconds.length()==1){
                        seconds = "0"+seconds;
                    }
                    findCountdownTimer(f).setText(minutes + ":" + seconds);
                    time = time - 1000;

                    try{
                    //do what you want to do before sleeping
                        Thread.sleep(1000);//sleep for 1000 ms
                    //do what you want to do after sleeptig
                    }
                    catch(Exception ie){
                    }
                }
                findDestInfo2(f).setEnabled(true);
                findDoneButton(f).setEnabled(true);
            }
        }
        Timer t = new Timer();
        TimerTask task = new MyTimerTask();
        t.schedule(task, 1000);
    }

    protected boolean onTransportingStopVehicle() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onTransportingStopVehicle();
        findDestInfo2(TransForm).setEnabled(true);
        findDoneButton(TransForm).setEnabled(true);
        return val;
    }

    protected boolean onReportProblemSend() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onReportProblemSend();
        findSendButton(Report).setText("Sent...");
        findSendButton(Report).setEnabled(false);
        findSentInfo(Report).setEnabled(true);
        return val;
    }

    protected void beforeReportProblem(Form f) {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        super.beforeReportProblem(f);
        Report = f;
        findSendButton(f).setEnabled(true);
        findSentInfo(f).setEnabled(false);
    }

    protected boolean onMainRetry() {
        // If the resource file changes the names of components this call will break notifying you that you should fix the code
        boolean val = super.onMainRetry();
        SysCon.connect();
        updateStatus(mainForm, SysCon);
        return val;
    }

    void updateStatus(Form f, SystemConnect sc){
        Label status = findSystemStatus(f);
        Label sm = findSystemMessage(f);
        Button r = findRetryButton(f);
        if(sc.e == false){
            status.setUIID("LabelStatusGood");
            status.setText("Available");
            sm.setEnabled(false);
            r.setEnabled(false);
            findPassCont(mainForm).setEnabled(true);
            findPickUpCont(mainForm).setEnabled(true);
            findDropOffCont(mainForm).setEnabled(true);
            findReqRide(mainForm).setEnabled(true);
            updateOptions();
        } else {
            status.setUIID("LabelStatusBad");
            status.setText("Unavailable");
            sm.setText(sc.systemMessage);
            sm.setEnabled(true);
            r.setEnabled(true);
            findNumPass(mainForm).setText("Unavailable");
            findPickUpLabel(mainForm).setText("Unavailable");
            findDropOffLabel(mainForm).setText("Unavailable");
            findPassCont(mainForm).setEnabled(false);
            findPickUpCont(mainForm).setEnabled(false);
            findDropOffCont(mainForm).setEnabled(false);
            findReqRide(mainForm).setEnabled(false);
        }
    }

    void updateOptions(){
        findNumPass(mainForm).setText(SysCon.passSelected);
        findPickUpLabel(mainForm).setText(SysCon.pickSelected);
        findDropOffLabel(mainForm).setText(SysCon.dropSelected);
    }
}